public class createNodeList {

    /**
     * 常规创建链表
     * @param nums
     * @return
     */
    public static ListNode createLinkedList(int[] nums) {
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        for (int num : nums) {
            curr.next = new ListNode(num);
            curr = curr.next;
        }
        return dummy.next;
    }

    /**
     * 创建带有环的链表
     * @param values
     * @param pos
     * @return
     */
    public static ListNode create(int[] values,int pos){
        ListNode head=createLinkedList(values); //创建头节点
        if(pos==0){
            return head;
        }
        ListNode curr=head; //当前的指针
        // 定位到第pos个节点
        int count=0;

        while (count < pos && curr.next != null) {
            curr = curr.next;
            count++;
        }

        // 记录第pos个节点的引用
        ListNode posNode = curr;

        // 遍历到链表尾部
        while (curr.next != null) {
            curr = curr.next;
        }

        // 将尾部节点的next指向第pos个节点
        curr.next = posNode;

        return head;
    }
}
